using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FolderSync
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
			this.Text = this.Text + " " + Program.Version;
		}

		private void openLeftFolder(object sender, EventArgs e)
		{
			if (folderBrowserDialog.ShowDialog(this) == DialogResult.OK) {
				leftFileListView.RootDirectory = folderBrowserDialog.SelectedPath;
			}
		}

		private void openRightFolder(object sender, EventArgs e)
		{
			if (folderBrowserDialog.ShowDialog(this) == DialogResult.OK) {
				rightFileListView.RootDirectory = folderBrowserDialog.SelectedPath;
			}
		}

		private void swapFileView(object sender, EventArgs e)
		{
			if (iconsToolStripMenuItem.Checked) {
				iconsToolStripMenuItem.Checked = false;
				detailsToolStripMenuItem.Checked = true;
				leftFileListView.View = View.Details;
				rightFileListView.View = View.Details;
			} else {
				iconsToolStripMenuItem.Checked = true;
				detailsToolStripMenuItem.Checked = false;
				leftFileListView.View = View.LargeIcon;
				rightFileListView.View = View.LargeIcon;
			}
		}

		private void leftFileListView_DirectoryChanged(object sender, EventArgs e)
		{
			leftUpButton.Enabled = leftFileListView.IsInSubdirectory;
			leftFolderTextBox.Text = leftFileListView.CurrentDirectory;
			RefreshStatus();
		}

		private void rightFileListView_DirectoryChanged(object sender, EventArgs e)
		{
			rightUpButton.Enabled = rightFileListView.IsInSubdirectory;
			rightFolderTextBox.Text = rightFileListView.CurrentDirectory;
			RefreshStatus();
		}

		private void RefreshStatus()
		{
			if (leftFileListView.Items.Count > 0 && rightFileListView.Items.Count > 0) {
				FileListView.AssignStatus(leftFileListView, rightFileListView);
				FileListView.AssignStatus(rightFileListView, leftFileListView);
			}
		}

		private void leftUpButton_Click(object sender, EventArgs e)
		{
			leftFileListView.GoUp();
			if (rightFileListView.IsInSubdirectory) {
				rightFileListView.GoUp();
			}
		}

		private void rightUpButton_Click(object sender, EventArgs e)
		{
			rightFileListView.GoUp();
		}

		private void leftRefreshButton_Click(object sender, EventArgs e)
		{
			leftFileListView.RefreshFiles();
			RefreshStatus();
		}

		private void rightRefreshButton_Click(object sender, EventArgs e)
		{
			rightFileListView.RefreshFiles();
			RefreshStatus();
		}

		private void syncButton_Click(object sender, EventArgs e)
		{
			//FileListView.AssignStatus(leftFileListView, rightFileListView);
			//FileListView.AssignStatus(rightFileListView, leftFileListView);
		}

		private void leftFileListView_DirectoryOpened(object sender, ListViewItemSelectionChangedEventArgs e)
		{
			ListViewItem item = rightFileListView.FindDirectoryItem(e.Item.Text);
			if (item != null) {
				rightFileListView.OpenSubdirectory(item);
			}
		}

		private void rightCopyButton_Click(object sender, EventArgs e)
		{
			if (rightFileListView.SelectedItems.Count > 0) {
				rightFileListView.CopySelectedFilesTo(leftFileListView.CurrentDirectory);
				leftFileListView.RefreshFiles();
				RefreshStatus();
			}
		}
		
		private void leftCopyButton_Click(object sender, EventArgs e)
		{
			if (leftFileListView.SelectedItems.Count > 0) {
				leftFileListView.CopySelectedFilesTo(rightFileListView.CurrentDirectory);
				rightFileListView.RefreshFiles();
				RefreshStatus();
			}
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			FolderPairsManager.Instance.Load();
			foreach (FolderPair pair in FolderPairsManager.Instance.FolderPairs) {
				folderPairsComboBox.Items.Add(pair);
			}
		}

		private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
		{
			using (AboutBox about = new AboutBox()) {
				about.ShowDialog(this);
			}
		}

		private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			FolderPairsManager.Instance.Save();
		}

		private void newFolderPairButton_Click(object sender, EventArgs e)
		{
			using (FolderPairDialog dialog = new FolderPairDialog()) {
				if (dialog.ShowDialog(this) == DialogResult.OK) {
					FolderPair pair = dialog.FolderPair;
					FolderPairsManager.Instance.Add(pair);
					folderPairsComboBox.Items.Add(pair);
					folderPairsComboBox.SelectedItem = pair;
				}
			}
		}

		private void folderPairsComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			FolderPair pair = folderPairsComboBox.SelectedItem as FolderPair;
			if (pair != null) {
				leftFileListView.RootDirectory = pair.LeftFolder;
				rightFileListView.RootDirectory = pair.RightFolder;
			}
		}

		private void deleteFolderPairButton_Click(object sender, EventArgs e)
		{
			FolderPair pair = folderPairsComboBox.SelectedItem as FolderPair;
			if (pair != null) {
				FolderPairsManager.Instance.Remove(pair.Name);
				folderPairsComboBox.Items.Remove(pair);
			}
		}
	}
}